package cn.th.controller;


import cn.th.common.R;
import cn.th.entity.Item;
import cn.th.entity.Myclass;
import cn.th.service.IItemService;
import cn.th.service.IMyclassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Slf4j
@RestController
@RequestMapping("/myclass")
public class MyclassController {

    @Autowired
    private IMyclassService myClassService;

    /**
     * 新增班级，采用excel表格形式导入
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R<String> uploadExcel(@RequestParam("file") MultipartFile file) {
        myClassService.saveFile(file);
        return R.success("新增班级成功！！！");
    }

    /**
     * 查询班级表
     * @param page
     * @param pageSize
     * @param school
     * @param name
     * @param academy
     * @param total
     * @return
     */
    @GetMapping("/page")
    public R<Map<String,Object>> page(@RequestParam(value = "page") int page,
                                      @RequestParam(value = "pageSize") int pageSize,
                                      @RequestParam(value = "school", required = false) String school,
                                      @RequestParam(value = "name", required = false) String name,
                                      @RequestParam(value = "academy", required = false) String academy,
                                      @RequestParam(value = "professional", required = false) String professional,
                                      @RequestParam(value = "total", required = false) String total){

        Map<String,Object> data = myClassService.queryMyClass(page, pageSize, school, name, academy, professional, total);

        return R.success(data,"查询班级表成功");
    }

    /**
     * 根据id删除班级表
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){
        log.info("删除物品表信息，id为：{}",ids);

        //删除班级表的数据，可批量删除
        myClassService.removeBatchByIds(ids);

        return R.success("删除班级表信息成功");
    }

    /**
     * 修改班级表
     * @param myclass
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Myclass myclass){
        log.info("修改班级表信息：{}",myclass);

        myClassService.updateById(myclass);

        return R.success("修改班级表成功");
    }

    /**
     * 修改班级时页面的数据展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Myclass> getMyclassById(@PathVariable("id")Long id){
        Myclass myclass = myClassService.getById(id);
        return R.success(myclass);
    }
}

